<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="content">
      <!-- 头部功能 -->
      <template #header>
        <div>
          <el-input
            class="search-input"
            placeholder="请输入内容"
            size="medium"
            clearable
            v-model="query"
            @clear="reloadUserlistQuery"
          >
            <template #append>
              <el-button
                icon="el-icon-search"
                @click="reloadUserlistQuery"
              ></el-button>
            </template>
          </el-input>
        </div>
      </template>
      <!-- 正文内容 -->
      <el-table
        :data="orders"
        style="width: 100%"
        border
        size="small"
        v-loading="loading"
        row-key="order_id"
        :expand-row-keys="expandKeys"
        @expand-change="openColumn"
      >
        <template #empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
        <el-table-column type="expand" label="明细" width="50px">
          <template>
            <el-table border style="width: 94%" class="goodsTab"
            :data="orderDetailsList"
            >
              <el-table-column label="封面" width="100">
                <template slot-scope="scope">
                  <img :src="scope.row.pic" class="detailPic">
                </template>
              </el-table-column>
              <el-table-column prop="name" label="商品名称"> </el-table-column>
              <el-table-column prop="weight" label="商品重量" width="100"> </el-table-column>
              <el-table-column prop="number" label="商品数量" width="100"> </el-table-column>
              <el-table-column prop="price" label="商品价格(元)" width="120">
              </el-table-column>
              <el-table-column prop="total" label="总价(元)" width="100"> </el-table-column>
            </el-table>
          </template>
        </el-table-column>

        <el-table-column
          type="index"
          label="序号"
          width="50px"
        ></el-table-column>
        <el-table-column
          label="订单编号"
          min-width="135px"
          prop="order_number"
        ></el-table-column>
        <el-table-column
          label="订单价格(元)"
          min-width="135px"
          prop="order_price"
        ></el-table-column>
        <el-table-column label="是否付款" width="120px" prop="pay_status">
          <template #default="{ row }">
            <el-tag
              effect="dark"
              :type="
                items[row.pay_status * 1]
                  ? items[row.pay_status * 1].type
                  : 'danger'
              "
            >
              {{
                items[row.pay_status * 1]
                  ? items[row.pay_status * 1].label
                  : "未付款"
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" width="120px" prop="is_send">
        </el-table-column>
        <el-table-column
          label="下单时间"
          width="200px"
          prop="create_time"
          :formatter="formatDate"
        >
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <el-tooltip effect="dark" content="修改订单地址" placement="bottom">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="modifyAddress(row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="订单物流信息" placement="bottom">
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-s-opportunity"
                @click="KuaidiDialog = true"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagesize"
        :total="total"
        :current-page="pagenum"
        @current-change="reloadUserlistPage"
        @size-change="reloadUserlistSize"
      >
      </el-pagination>
    </el-card>
    <!-- 弹层 -->
    <ModifyAddress
      :orderId="orderId"
      :visible.sync="modifyAddressDialog"
      @handleModifyAddress="handleModifyAddress"
    ></ModifyAddress>
    <KuaiDi :visible.sync="KuaidiDialog"></KuaiDi>
  </div>
</template>

<script>
import filters from "@/filters";
import ModifyAddress from "@/components/orderPage/ModifyAddress";
import KuaiDi from "@/components/orderPage/KuaiDi";
export default {
  components: {
    ModifyAddress,
    KuaiDi,
  },
  data() {
    return {
      loading: false,
      total: 0,
      pagesize: 10,
      pagenum: 1,
      query: "",
      orders: [],
      orderDetailsList:[],
      items: [
        { type: "danger", label: "未付款" },
        { type: "success", label: "已付款" },
      ],
      modifyAddressDialog: false,
      orderId:0,
      KuaidiDialog: false,
      expandKeys: [],
    };
  },
  methods: {
    reloadUserlistPage(newPageNum) {
      // console.log(this.pagenum,newPageNum);
      this.pagenum = newPageNum;
      this.initOrderList();
    },
    reloadUserlistSize(newPageSize) {
      // console.log(this.pagesize,newPageSize);
      this.pagenum = 1;
      this.pagesize = newPageSize;
      this.initOrderList();
    },
    reloadUserlistQuery() {
      this.pagenum = 1;
      this.initOrderList();
    },
    async initOrderList() {
      try {
        this.loading = true;
        let result = await this.$api.getOrderList({
          query: this.query,
          pagenum: this.pagenum,
          pagesize: this.pagesize,
        });
        if (result == "error") return;
        // console.log("111",result.goods);
        this.total = result.total;
        this.pagenum = result.pagenum * 1;
        this.orders = result.goods;
      } catch (error) {
        console.log("订单列表加载错误:", error);
      } finally {
        this.loading = false;
      }
    },
    formatDate(row, column) {
      //   console.log(row, column);
      let date = row[column.property];
      //   console.log(date, filters.formatDate(date, "yyyy-MM-dd HH:mm:ss"));
      return filters.formatDate(date, "yyyy-MM-dd HH:mm:ss");
    },
    async deleteGoods(good_id) {
      try {
        let result = await this.$api.deleteGoodsByGid(good_id);
        if (result == "error") return;

        this.initOrderList();
      } catch (error) {
        console.log("商品删除错误:", error);
      }
    },
    handleModifyAddress(dialogState) {
      // console.log(dialogState);
      this.modifyAddressDialog = dialogState;
    },
    modifyAddress(row){
      this.orderId = row.order_id;
      this.modifyAddressDialog = true;
    },
    async openColumn(row) {
      try {
        const rowId = this.expandKeys[0]?this.expandKeys[0]:'';
        if(rowId == row.order_id){
          this.expandKeys = [];
          return
        }
        const groupId = row.order_id;
        this.expandKeys = [groupId];


        this.orderDetailsList=[];
        let result = await this.$api.getOrderDetails(row.order_id)
        console.log(result);
        if(result=='error') return
        for (let good of result.goods) {
          let goodResult = await this.$api.getGoodsDetails(good.goods_id)
          console.log(goodResult);
          let goosItem = {
            pic:goodResult.pics[0].pics_sma_url,
            name:goodResult.goods_name,
            number:good.goods_number,
            price:good.goods_price,
            weight:goodResult.goods_weight,
            total:good.goods_total_price
          }
          this.orderDetailsList.push(goosItem);
        }
        console.log(this.orderDetailsList);
      } catch (error) {
        console.log("订单详情数据加载错误:", error);
      }
    },
  },
  created() {
    this.initOrderList();
  },
};
</script>


<style lang="less" scoped>
.content,
.el-pagination {
  margin-top: 20px;
}
.el-card {
  .el-table {
    font-size: 14px;
    .goodsTab {
      margin: 20px 3%;
      .detailPic{
        width: 50px;
        height: 50px;
      }
    }
  }
}
.search-input {
  width: 300px;
}
.add-btn {
  margin-left: 20px;
}
</style>